<?php
namespace api\common\model;
use think\Db;
class StaffStatus extends \think\Model 
{
    protected $autoWriteTimestamp = true;
    protected $table = 'staff_status';

    /**
     * 查看员工状态
     */
    public function selStatus($staffId){
        $sel = Db::name($this->table)->field('status')->where('staffId',$staffId)->find();
        return $sel;
    }

    /**
     * 查看员工上班状态
     * @param $staffId 员工id
     * @return $data
     */
    public function selWorking($staffId){
        $sel = Db::query('SELECT
        t1.shopId,
        t2.shopName,
        t2.type
    FROM
        mj_staff_status  as t1
        inner join mj_shop as t2 on t1.shopId = t2.shopId
    WHERE
         
         t1.STATUS = 1 and t1.staffId = '.$staffId.' limit 0,1');
        return $sel[0];
    }

    /**
     * 员工上班
     * @param $staffId 员工id
     * @param $shopId 店铺id
     * @return bool\data
     */
    public function toWork($staffId,$shopId){
        $date = getNowDate();
        $data['addtime'] = $date;
        $data['updtime'] = $date;
        $data['status'] = 1;
        $data['shopId'] = $shopId;
        $data['staffId'] = $staffId;
        Db::startTrans();
        $transStatus = false;
        try{
            // 员工上班
            $work = Db::name('staff_status')->insert($data);
            // 判断是否上班
            if (!$work) {
                throw new \Exception("上线失败1");
            }
            // 门店服务人员增加
            $server = Db::name('shop_server')->where('shopid',$shopId)->setInc('numbers');
            if (!$server) {
                throw new \Exception("门店服务人员增加失败");
            }
            // 查询门店
            $sel_server = Db::query('SELECT
                    t2.shopId,
                    t2.shopName,
                    t2.address,
                    t2.status
                FROM
                    mj_staff_status as t1
                    left join mj_shop as t2 on t1.shopId = t2.shopId
                WHERE
                    t1.status = 1 and t1.shopId = '.$shopId.' limit 1');
            if (!$sel_server) {
                throw new \Exception("门店服务人员查询失败");
            }
            $transStatus = $sel_server;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            exit(json_encode(['code'=>"-1",'message'=>$e->getMessage(),'data'=>null]));
            Db::rollback();
        }
        return $transStatus;
    }

    /**
     * 员工下班
     * @param $staffId 员工id
     * @param $shopId 店铺id
     * @return bool\data
     */
    public function downWork($staffId,$shopId){
        $date = getNowDate();
        $data['updtime'] = $date;
        $data['status'] = 2;
        $data['shopId'] = $shopId;
        $data['staffId'] = $staffId;
        Db::startTrans();
        $transStatus = false;
        try{
            // 员工下班
            $work =  Db::name('staff_status')->where('staffId',$staffId)->where('shopId',$shopId)->update($data);
            // 判断是否下班
            if (!$work) {
                throw new \Exception("下线失败");
            }
            
            // 门店服务人员增加
            $server = Db::name('shop_server')->where('shopid',$shopId)->setDec('numbers');
            if (!$server) {
                throw new \Exception("门店服务人员减少失败");
            }
            // 判断门店服务人员
            $serverNum = Db::name('shop_server')->where('shopid',$shopId)->field('numbers')->find();
            if($serverNum['numbers'] == 0){
                $data_ser['numbers'] = 0;
                $data_ser['serveringNumbers'] = 0;
                $data_ser['waitingServer'] = 0;
                $data_ser['updateTime'] = getNowdate();
                $upd = Db::name('shop_server')->where('shopid',$shopId)->update($data_ser);
                if (!$upd) {
                    throw new \Exception("清空门店服务人数失败");
                }
            }
            $transStatus = true;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            \think\Log::record($e->getMessage());
            exit(json_encode(['code'=>"-1",'message'=>$e->getMessage(),'data'=>null]));
            // 回滚事务
            Db::rollback();
        }
        return $transStatus;
    }

}
